package com.likui.myqq.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.likui.myqq.R;
import com.likui.myqq.model.RecentChat;
import com.likui.myqq.util.ImageUtil;
import com.likui.myqq.util.SystemMethod;
import com.likui.myqq.view.CustomListView;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.List;

/**
 * Created by helikui on 2015/10/4.
 */
public class QQContactAdapter extends BaseAdapter {
    private static final String TAG = "NewsAdapter";
    private Context mContext;
    private List<RecentChat> lists;
    private CustomListView mCustomListView;
    private HashMap<String, SoftReference<Bitmap>> hashMaps = new HashMap<String, SoftReference<Bitmap>>();

    public QQContactAdapter(Context mContext, List<RecentChat> lists, CustomListView mCustomListView) {
        this.mContext = mContext;
        this.lists = lists;
        this.mCustomListView = mCustomListView;
    }

    @Override
    public int getCount() {
        if (lists != null) {
            return lists.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Holder holder;
        RecentChat chat = lists.get(position);
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.fragment_news_qq_item, null);
            holder = new Holder();
            holder.iv = (ImageView) convertView.findViewById(R.id.user_picture);
            holder.tv_name = (TextView) convertView.findViewById(R.id.user_name);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        String path = chat.getImgPath();
        if (hashMaps.containsKey(path)) {
            holder.iv.setImageBitmap(hashMaps.get(path).get());
            //另一个地方缓存释放资源
            ImageUtil.getInstance().removeCache(path);
        } else {
            holder.iv.setTag(chat.getImgPath());
            ImageUtil.getInstance().loadBitmap(chat.getImgPath(), new ImageUtil.OnLoadBitmapListener() {
                @Override
                public void onLoadBitmap(String path, Bitmap bitmap) {
                    ImageView iv = (ImageView) mCustomListView.findViewWithTag(path);
                    if (bitmap != null && iv != null) {
                        bitmap = SystemMethod.toRoundCorner(bitmap, 15);
                        iv.setImageBitmap(bitmap);

                        if (!hashMaps.containsKey(path)) {
                            hashMaps.put(path, new SoftReference<Bitmap>(bitmap));
                        }
                    }
                }
            });
        }

        holder.tv_name.setText(chat.getUserName());
        return convertView;
    }

    class Holder {
        ImageView iv;
        TextView tv_name;
    }
}
